package communicationMessages;

import java.sql.ResultSet;
import java.util.logging.Level;

import logger.LoggerWriter;
import data.Member;
import data.MemberDataBase;
import database.DB;

public class MsgsByUserMessage extends Message { //return a list of users

	@Override
	public String getAnswer() {
		String []arr = this.content.split(delimiter);
		LoggerWriter logger = LoggerWriter.getLoggerWriter();
		if (arr.length != 1)
		{
			logger.log("failed"+"MsgsByUserMessage wrong format"+delimiter, Level.WARNING);
			return "failed"+delimiter+"MsgsByUserMessage wrong format"+delimiter;
		}
		String answer = "";
		String userName = arr[0];
		String userRoll = DB.getUserRoll(userName);
		boolean isAdmin = DB.isAdmin(userName);
		if (!isAdmin)
		{
			logger.log("not an admin try get num of msgs by user", Level.WARNING);
			return "failed" + delimiter + "not an admin try get num of msgs by user" + delimiter;
		}
		else
		{
			answer = "msgsByUser" + delimiter + userRoll + delimiter;
			ResultSet res = DB.getAllUsers();   //method already exist
			answer = getUsersNameFromResultSet(answer, res);

		}
		return answer;
	}

}	
/*
 * String userRoll = MemberDataBase.getUserRoll(userName);
		MemberDataBase membersDataBase = MemberDataBase.getMembersDataBase();
		Member member = membersDataBase.getMember(userName);
		boolean isAdmin = member.get_isAdmin();
		if (!isAdmin)
		{
			logger.log("not an admin try get num of msgs by user", Level.WARNING);
			return "failed" + delimiter + "not an admin try get num of msgs by user" + delimiter;
		}
		else
		{
			ans = "msgsByUser" + delimiter + userRoll + delimiter;
			for(String name :membersDataBase.getUsers().keySet())
			{
				ans+=name + delimiter;
			}
		}
		return ans;
		*/
 